﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ElasticAgTest.Models
{
    public static class PeaksTroughsHelper
    {
        public static void FindPeaksTroughs(double[] v, ref List<int> Peeks, ref List<int> Troughs)
        {
            int[] diff_v = new int[v.Length - 1];
            //计算v的一阶差分和符号函数trend
            for (int i = 0; i < diff_v.Length; i++)
            {
                if (v[i + 1] > v[i])
                {
                    diff_v[i] = 1;
                }
                else if (v[i + 1] < v[i])
                {
                    diff_v[i] = -1;
                }
                else
                    diff_v[i] = 0;
            }
            //对Trend做一个遍历
            for (int i = diff_v.Length - 1; i >= 0; i--)
            {
                if (diff_v[i] == 0 && i == diff_v.Length - 1)
                {
                    diff_v[i] = 1;
                }
                else if (diff_v[i] == 0)
                {
                    if (diff_v[i + 1] >= 0)
                    {
                        diff_v[i] = 1;
                    }
                    else
                    {
                        diff_v[i] = -1;
                    }
                }
            }
            for (int i = 0; i < diff_v.Length - 1; i++)
            {
                if (diff_v[i + 1] - diff_v[i] == -2)
                {
                    Peeks.Add(i + 1);
                }
                else if (diff_v[i + 1] - diff_v[i] == 2)
                {
                    Troughs.Add(i + 1);
                }
            }
        }
    }
}
